package com.fanshuai.spring.boot.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 由于未知原因，DataSourceConfig @Value注入失败，此处做后置处理
 */
@Component
public class ConfigPostProcessor {
    @Value("${db.test.url}")
    private String urlTest;

    @Value("${db.test.user}")
    private String usernameTest;

    @Value("${db.test.password}")
    private String passwordTest;

    @Value("${db.test2.url}")
    private String urlTest2;

    @Value("${db.test2.user}")
    private String usernameTest2;

    @Value("${db.test2.password}")
    private String passwordTest2;

    @Value("${druid.xa.enabled:false}")
    private boolean druidXAEnabled;

    @Resource
    private DruidDataSource dataSource1;
    @Resource
    private DruidDataSource dataSource2;

    @PostConstruct
    public void init() {
        if (null != dataSource1) {
            dataSource1.setUrl(urlTest);
            dataSource1.setUsername(usernameTest);
            dataSource1.setPassword(passwordTest);
        }
        if (null != dataSource2) {
            dataSource2.setUrl(urlTest2);
            dataSource2.setUsername(usernameTest2);
            dataSource2.setPassword(passwordTest2);
        }
    }
}
